EmSAT Achieve Exam  >  EmSAT Achieve Questions  >  What will be the output of the following code... Start Learning for Free
What will be the output of the following code?
```java
int n = 3;
int i = 0;
do {
    System.out.print(n + " ");
    n--;
    i++;
} while (i < n);
```
  • a)
    3 2 1
  • b)
    3 2
  • c)
    3 1
  • d)
    3
Correct answer is option 'A'. Can you explain this answer?
Most Upvoted Answer
What will be the output of the following code?```javaint n = 3;int i =...
The code prints the values of 'n' from 3 to 1 because the condition 'i < n' is true for those values. The loop terminates when 'i' becomes 3, and the condition becomes false.
Free Test
Community Answer
What will be the output of the following code?```javaint n = 3;int i =...
Understanding the Code
The given Java code snippet is a simple do-while loop that prints the value of `n` and decrements it until a certain condition is met. Let's break it down step by step.
Code Breakdown
- Initialization:
- `int n = 3;` initializes `n` to 3.
- `int i = 0;` initializes `i` to 0.
- Do-While Loop:
- The loop starts with `do { ... } while (i < />
- Inside the loop, it prints the current value of `n` followed by a space.
Loop Execution
1. First Iteration:
- `n` is 3, `i` is 0.
- Prints `3`.
- Decrements `n` to 2 and increments `i` to 1.
2. Second Iteration:
- `n` is now 2, `i` is 1.
- Prints `2`.
- Decrements `n` to 1 and increments `i` to 2.
3. Third Iteration:
- `n` is 1, `i` is 2.
- The condition `i < n`="" (2="" />< 1)="" is="" now="" false,="" so="" the="" loop="" />
Final Output
The output of the code will be:
- `3 2 `
Thus, the correct answer is option 'A': 3 2 1. The code only prints `3` and `2` because the loop exits before it can print `1`.
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Question Description
What will be the output of the following code?```javaint n = 3;int i = 0;do { System.out.print(n + " "); n--; i++;} while (i < n);```a)3 2 1b)3 2c)3 1d)3Correct answer is option 'A'. Can you explain this answer? for EmSAT Achieve 2025 is part of EmSAT Achieve preparation. The Question and answers have been prepared according to the EmSAT Achieve exam syllabus. Information about What will be the output of the following code?```javaint n = 3;int i = 0;do { System.out.print(n + " "); n--; i++;} while (i < n);```a)3 2 1b)3 2c)3 1d)3Correct answer is option 'A'. Can you explain this answer? covers all topics & solutions for EmSAT Achieve 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What will be the output of the following code?```javaint n = 3;int i = 0;do { System.out.print(n + " "); n--; i++;} while (i < n);```a)3 2 1b)3 2c)3 1d)3Correct answer is option 'A'. Can you explain this answer?.
Solutions for What will be the output of the following code?```javaint n = 3;int i = 0;do { System.out.print(n + " "); n--; i++;} while (i < n);```a)3 2 1b)3 2c)3 1d)3Correct answer is option 'A'. Can you explain this answer? in English & in Hindi are available as part of our courses for EmSAT Achieve. Download more important topics, notes, lectures and mock test series for EmSAT Achieve Exam by signing up for free.
Here you can find the meaning of What will be the output of the following code?```javaint n = 3;int i = 0;do { System.out.print(n + " "); n--; i++;} while (i < n);```a)3 2 1b)3 2c)3 1d)3Correct answer is option 'A'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What will be the output of the following code?```javaint n = 3;int i = 0;do { System.out.print(n + " "); n--; i++;} while (i < n);```a)3 2 1b)3 2c)3 1d)3Correct answer is option 'A'. Can you explain this answer?, a detailed solution for What will be the output of the following code?```javaint n = 3;int i = 0;do { System.out.print(n + " "); n--; i++;} while (i < n);```a)3 2 1b)3 2c)3 1d)3Correct answer is option 'A'. Can you explain this answer? has been provided alongside types of What will be the output of the following code?```javaint n = 3;int i = 0;do { System.out.print(n + " "); n--; i++;} while (i < n);```a)3 2 1b)3 2c)3 1d)3Correct answer is option 'A'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What will be the output of the following code?```javaint n = 3;int i = 0;do { System.out.print(n + " "); n--; i++;} while (i < n);```a)3 2 1b)3 2c)3 1d)3Correct answer is option 'A'. Can you explain this answer? tests, examples and also practice EmSAT Achieve tests.
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev